package com.josephxu.tank.strategy;

/**
 * @Author: Joseph Xu
 * @Date: 2022/1/25 21:24
 */
public class Sorter<T> {

    public void sort(T[] arr,Comparator<T> comparator){
        boolean hasChange = true;
        if (arr == null || arr.length == 0){
            return;
        }

        for (int i = 0;i<arr.length-1&&hasChange;i++){
            hasChange = false;
            for (int j = arr.length -1;j>0;j--){

                if (comparator.compare(arr[j],arr[j-1])<0){
                    swap(arr,j,j-1);
                    hasChange=true;
                }
            }
        }

    }

    public void swap(T[] arr, int i , int j){
        T temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

}
